package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemRoleDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.SystemRoleDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemRoleRequestDTO;
import com.example.child_protection.dto.response.SystemRoleListResponseDTO;
import com.example.child_protection.dto.response.SystemRoleResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemRoleModel;
import com.example.child_protection.service.SystemRoleQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemRoleQueryServiceImpl extends MyBaseService implements SystemRoleQueryService {

    @Autowired
    public SystemRoleDao systemRoleDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRoleResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_ROLE_FIND_ID, requestDTO, SystemRoleResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemRoleResponseDTO>() {
            @Override
            protected SystemRoleResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemRoleResponseDTO responseDTO = new SystemRoleResponseDTO();
                SystemRoleModel systemRoleModel = systemRoleDao.findId(requestDTO.getId());
                SystemRoleDTO systemRole = new SystemRoleDTO();
                BeanUtils.copyProperties(systemRoleModel, systemRole);
                responseDTO.setInfo(systemRole);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRoleListResponseDTO findList(SystemRoleRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_ROLE_FIND_LIST, requestDTO, SystemRoleListResponseDTO.class, new MyServiceExecutor<SystemRoleRequestDTO, SystemRoleListResponseDTO>() {
            @Override
            protected SystemRoleListResponseDTO myprocess(SystemRoleRequestDTO requestDTO) throws Exception {
                SystemRoleListResponseDTO responseDTO = new SystemRoleListResponseDTO();
                SystemRoleModel req = new SystemRoleModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemRoleModel> lists = systemRoleDao.findPage(req);
                List<SystemRoleDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemRoleDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
